﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.Views
{
    public static class PreloadModule
    {
        private static List<PreloadPartInfo> s_preloadParts = new List<PreloadPartInfo>();

        /// <summary>
        /// 对solution里的part进行预加载操作
        /// </summary>
        /// <param name="solution"></param>
        public static void Process(CPPEI.Coolzon.Metadata.Solution solution)
        {
            if (UIContext.Current.IsShowActiveXPart)
            {
                ScanSolution(solution);

                try
                {
                    Assembly assembly = Assembly.Load("CPPEI.Coolzon.UI.ActiveXPartLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");//.LoadFile(string.Format("{0}\\CPPEI.Coolzon.UI.ActiveXPartLib.dll", AppDomain.CurrentDomain.BaseDirectory));
                    // 
                    for (int index = 0; index < s_preloadParts.Count; index++)
                    {
                        CPPEI.Coolzon.Common.Logger.InfoToTag("UI.Views", "预加载组件,scene:{0},part:{1}",
                            s_preloadParts[index].Scene.Name,
                            s_preloadParts[index].Part.Name);

                        IPreloader preloader = assembly.CreateInstance("CPPEI.Coolzon.UI.ActiveXPartLib.INSVideoPreloader") as IPreloader;

                        preloader.Start(s_preloadParts[index].Scene, s_preloadParts[index].Part);
                        PreloadManager.Save(s_preloadParts[index].Scene.Name,
                            s_preloadParts[index].Part.Name, preloader);
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        public static void Release()
        {
            s_preloadParts.Clear();

            for (int index = 0; index < PreloadManager.InfoList.Count; index++)
            {
                PreloadManager.InfoList[index].Preloader.Stop();
            }
            PreloadManager.Clear();
        }

        private static void ScanSolution(CPPEI.Coolzon.Metadata.Solution solution)
        {
            if (solution.Scenes != null)
            {
                for (int index = 0; index < solution.Scenes.Count; index++)
                {
                    ScanScene(solution.Scenes[index]);
                }
            }
        }

        private static void ScanScene(CPPEI.Coolzon.Metadata.Scene scene)
        {
            if (scene.Scenes != null)
            {
                for (int index = 0; index < scene.Scenes.Count; index++)
                {
                    ScanScene(scene.Scenes[index]);
                }
            }

            if (scene.Areas != null)
            {
                for (int areaIndex = 0; areaIndex < scene.Areas.Count; areaIndex++)
                {
                    ScanArea(scene, scene.Areas[areaIndex]);
                }
            }
        }

        private static void ScanArea(CPPEI.Coolzon.Metadata.Scene scene, CPPEI.Coolzon.Metadata.Area area)
        {
            if (area.Areas != null)
            {
                for (int areaIndex = 0; areaIndex < area.Areas.Count; areaIndex++)
                {
                    ScanArea(scene, area.Areas[areaIndex]);
                }
            }

            if (area.Parts != null)
            {
                for (int partIndex = 0; partIndex < area.Parts.Count; partIndex++)
                {
                    ScanPart(scene, area.Parts[partIndex]);
                }
            }
        }

        private static void ScanPart(CPPEI.Coolzon.Metadata.Scene scene, CPPEI.Coolzon.Metadata.Part part)
        {
            //if (part.Type == "INSVideoPart")// && part.IsPreload)
            //{
            //    int rd = new Random().Next(0, 3);
            //    if (rd == 2)
            //    {
            //        part.IsPreload = true;
            //        s_preloadParts.Add(new PreloadPartInfo(scene, part));
            //    }
            //    System.Threading.Thread.Sleep(50);
            //}
            if (part.Type == "INSVideoPart" && part.IsPreload)
            {
                CPPEI.Coolzon.Common.Logger.InfoToTag("UI.View", string.Format("发现视频预加载组件,scene:{0},part:{1}", scene.Name, part.Name));
                s_preloadParts.Add(new PreloadPartInfo(scene, part));
            }

            if (part.Parts != null)
            {
                for (int partIndex = 0; partIndex < part.Parts.Count; partIndex++)
                {
                    ScanPart(scene, part.Parts[partIndex]);
                }
            }
        }

        class PreloadPartInfo
        {
            public CPPEI.Coolzon.Metadata.Scene Scene { get; set; }
            public CPPEI.Coolzon.Metadata.Part Part { get; set; }

            public PreloadPartInfo(CPPEI.Coolzon.Metadata.Scene scene, CPPEI.Coolzon.Metadata.Part part)
            {
                this.Scene = scene;
                this.Part = part;
            }
        }
    }
}
